package cn.iocoder.yudao.module.reservation.service.accountbalance;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.reservation.controller.admin.accountbalance.vo.AccountBalancePageReqVO;
import cn.iocoder.yudao.module.reservation.controller.admin.accountbalance.vo.AccountBalanceSaveReqVO;
import cn.iocoder.yudao.module.reservation.dal.dataobject.accountbalance.AccountBalanceDO;
import cn.iocoder.yudao.module.reservation.dal.mysql.accountbalance.AccountBalanceMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.ACCOUNT_BALANCE_NOT_EXISTS;

/**
 * 会员余额 Service 实现类
 *
 * @author yy
 */
@Service
@Validated
public class AccountBalanceServiceImpl implements AccountBalanceService {

    @Resource
    private AccountBalanceMapper accountBalanceMapper;

    @Override
    public Long createAccountBalance(AccountBalanceSaveReqVO createReqVO) {
        // 插入
        AccountBalanceDO accountBalance = BeanUtils.toBean(createReqVO, AccountBalanceDO.class);
        accountBalanceMapper.insert(accountBalance);
        // 返回
        return accountBalance.getId();
    }

    @Override
    public void updateAccountBalance(AccountBalanceSaveReqVO updateReqVO) {
        // 校验存在
        validateAccountBalanceExists(updateReqVO.getId());
        // 更新
        AccountBalanceDO updateObj = BeanUtils.toBean(updateReqVO, AccountBalanceDO.class);
        accountBalanceMapper.updateById(updateObj);
    }

    @Override
    public void deleteAccountBalance(Long id) {
        // 校验存在
        validateAccountBalanceExists(id);
        // 删除
        accountBalanceMapper.deleteById(id);
    }

    private void validateAccountBalanceExists(Long id) {
        if (accountBalanceMapper.selectById(id) == null) {
            throw exception(ACCOUNT_BALANCE_NOT_EXISTS);
        }
    }

    @Override
    public AccountBalanceDO getAccountBalance(Long id) {
        return accountBalanceMapper.selectById(id);
    }

    @Override
    public PageResult<AccountBalanceDO> getAccountBalancePage(AccountBalancePageReqVO pageReqVO) {
        return accountBalanceMapper.selectPage(pageReqVO);
    }

}